package com.oa.srms.apps.business.organization.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.business.organization.ISectionCategoryService;
import com.oa.srms.apps.business.organization.ISectionInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.organization.ISectionInfoDao;
import com.oa.srms.apps.entity.organization.SectionInfoValue;

public class SectionInfoServiceImpl implements ISectionInfoService {
	@Autowired
	private ISectionCategoryService sectionCategoryService;
	@Autowired
	private SectionInfoValue sectionInfo;
	@Autowired
	private ISectionInfoDao infoDao;

	@Override
	public int addSectionInfo(int categoryId, String sectionNo,
			String sectionName) throws Exception {
		sectionInfo.setSectionCategory(sectionCategoryService.queryByCategoryId(categoryId));
		if(sectionInfo.getSectionCategory() != null){
			sectionInfo.setSectionNo(sectionNo);
			sectionInfo.setSectionName(sectionName);
			sectionInfo.setStates(IDEDStates.ENABLE);
			return infoDao.addSectionInfo(sectionInfo);
		}
		return 0;
	}

	@Override
	public boolean deleteSectionInfo(String sectionIds) throws Exception {
		String[] sectionIdStr = sectionIds.split(",");
		for(String sectionId : sectionIdStr){
			sectionInfo = queryBySectionId(Integer.parseInt(sectionId));
			if(sectionInfo != null){
				sectionInfo.setStates(IDEDStates.DELETE);
				infoDao.editSectionInfo(sectionInfo);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifySectionInfo(int sectionId, int categoryId,
			String sectionNo, String sectionName) throws Exception {
		sectionInfo = queryBySectionId(sectionId);
		if(sectionInfo != null){
			sectionInfo.setSectionCategory(sectionCategoryService.queryByCategoryId(categoryId));
			if(sectionInfo.getSectionCategory() != null){
				sectionInfo.setSectionNo(sectionNo);
				sectionInfo.setSectionName(sectionName);
				return infoDao.editSectionInfo(sectionInfo);
			}
			return false;
		}
		return false;
	}

	@Override
	public List<SectionInfoValue> queryAllSectionInfo() throws Exception {
		return infoDao.querySectionInfo(sectionInfo);
	}

	@Override
	public SectionInfoValue queryBySectionId(int sectionId) throws Exception {
		sectionInfo.setSectionId(sectionId);
		return infoDao.queryBySectionId(sectionInfo);
	}
	
	

	@Override
	public List<SectionInfoValue> querySectionInfo(Integer categoryId,
			Integer states) throws Exception {
		if(categoryId != null){
			sectionInfo.setSectionCategory(sectionCategoryService.queryByCategoryId(categoryId));
		}
		sectionInfo.setStates(states);
		return infoDao.querySectionInfo(sectionInfo);
	}

	@Override
	public boolean toDisable(int sectionId) throws Exception {
		sectionInfo = queryBySectionId(sectionId);
		if(sectionInfo != null){
			sectionInfo.setStates(IDEDStates.DISABLE);
			return infoDao.editSectionInfo(sectionInfo);
		}
		return false;
	}

	@Override
	public boolean toEnable(int sectionId) throws Exception {
		sectionInfo = queryBySectionId(sectionId);
		if(sectionInfo != null){
			sectionInfo.setStates(IDEDStates.ENABLE);
			return infoDao.editSectionInfo(sectionInfo);
		}
		return false;
	}

}
